from django.core.files.storage import default_storage
from django.db.models import Q

from rest_framework.generics import ListAPIView, GenericAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.views import APIView


from course import courseser, models
from course.models import CourseModel, CategoryModel
from utils.myredis import redistoll


# Create your views here.
class Category(APIView):
    def get(self,request):
        allcate = models.CategoryModel.objects.filter(parent=None,ishome=True,isshow=True).order_by('order')
        ser = courseser.CategorySerializer(allcate,many=True)
        for i in ser.data:
            i['children']=i['children'][:3]
        return Response({"msg":"ok","data":ser.data,"code":200})


class NowCate(APIView):
    def get(self,request):
        id = request.query_params.get('id')
        info = models.CategoryModel.objects.filter(id=id,parent=None,ishome=True,isshow=True).first()
        ser = courseser.CategorySerializer(info)

        twocate = models.CategoryModel.objects.filter(parent=id,ishome=True).values_list('id',flat=True)
        course = models.CourseModel.objects.filter(category__in=twocate,isshow=True,ishome=True).order_by('order').values()[:4]
        courseinfo=ser.data
        courseinfo['course']=course
        return Response({"msg":"ok","data":courseinfo,"code":200})


class Direction(APIView):
    def get(self,request):
        allcate = models.CategoryModel.objects.filter(parent=None,ishome=True,isshow=True).order_by('order')
        ser = courseser.Category(allcate,many=True)
        return Response({"msg":'ok',"data":ser.data,"code":200})

class ChildCate(APIView):
    def get(self,request):
        pid = int(request.query_params.get('pid'))
        if pid==0:
            child = models.CategoryModel.objects.exclude(parent=None,isshow=False).order_by('order')
            print(child)
        else:
            child = models.CategoryModel.objects.filter(parent=pid,ishome=True,isshow=True).order_by('order')
        ser = courseser.Category(child,many=True)
        return Response({"msg":'ok',"data":ser.data,"code":200})


class CoursePage(PageNumberPagination):
    page_size = 3
    page_size_query_param = 'size'
    page_query_param = 'page'

class Courselist(ListAPIView):
    queryset = CourseModel.objects.all()
    serializer_class = courseser.CourseSerializer
    pagination_class = CoursePage

    def get_queryset(self):
        direction = self.request.query_params.get('direction')
        cate = self.request.query_params.get('cate')
        level = self.request.query_params.get('level',0)
        order = self.request.query_params.get('order')
        query=Q()
        if cate!='0':
            query&=Q(category=cate)
        if direction!='0' and cate=='0':
            allcate = CategoryModel.objects.filter(parent=direction).values()
            child=[]
            for i in allcate:
                child.append(i['id'])
                query&= Q(category_in=child)
        if level != '0' :
            query &= Q(level=level)
        if order:
            return self.queryset.filter(query).order_by(order)
        else:
            return self.queryset.filter(query)

class Detail(APIView):
    def get(self,request):
        id = request.query_params.get('id')
        info = models.CourseModel.objects.filter(id=id).first()
        info.read+=1
        info.save()
        ser = courseser.CourseSerializer(info)
        return Response({"msg":'ok',"data":ser.data,"code":200})

from drf_haystack.viewsets import HaystackViewSet

class HotWordView(HaystackViewSet):
    index_models = [models.CourseModel]
    serializer_class = courseser.CourseIndexSerializer
    pagination_class = CoursePage
    def list(self, request, *args, **kwargs):
        text=request.query_params.get('text','')
        if text:
            redistoll.hotadd('1hot',text)
        data=super().list(request).data
        for i in data['results']:
            i['cover']='http://127.0.0.1:8000/uploads/'+i['cover']
        return Response(data)

class HotWord(APIView):
    def get(self,request):
        data=redistoll.hotget('1hot',4)
        return Response(data)